package com.jyl.sort;

import java.util.Arrays;

/**
 * 选择排序
 * @ClassName SelectionSort
 * @Author admin
 * @Date 2024/6/14
 */
public class SelectionSort {

    public static void main(String[] args) {
        int[] arr = {3,1,5,4,7,6,2};
        selectionSort(arr);
        System.out.println(Arrays.toString(arr));

    }

    /**
     * 时间复杂度：O(n²)
     * 空间复杂度：O(1)
     * @param arr
     */
    protected static void selectionSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int target = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[target] > arr[j]) {
                    target = j;
                }
            }
            if (i != target) {
                int temp = arr[i];
                arr[i] = arr[target];
                arr[target] = temp;
            }
        }
    }



}
